﻿package com.tudou.home.model.ad.base
{
    import com.tudou.ad.interfaces.*;
    import com.tudou.model.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;
    import org.puremvc.as3.patterns.proxy.*;

    public class AdBaseProxy extends Proxy implements IAdProxy
    {
        protected var delayTimer:Timer;
        protected var _adItemInfo:IAdItemInfo;
        public var loadSize:uint = 0;
        protected var _adThrowInfo:IAdThrowInfo;
        protected var loader:Loader;
        protected var baseInfo:IBaseInfo;
        public var totalSize:uint = 0;

        public function AdBaseProxy(param1:String, param2:IBaseInfo)
        {
            super(param1);
            this.baseInfo = param2;
            return;
        }// end function

        public function get primaryAd() : DisplayObject
        {
            return loader.content;
        }// end function

        public function stopLoad() : void
        {
            _adItemInfo = null;
            loadSize = 0;
            totalSize = 0;
            removeLoader();
            removeDelayTimer();
            return;
        }// end function

        protected function completeHandler(event:Event) : void
        {
            var _loc_2:* = loader.content;
            return;
        }// end function

        public function get adThrowInfo() : IAdThrowInfo
        {
            return _adThrowInfo;
        }// end function

        protected function progressHandler(event:ProgressEvent) : void
        {
            loadSize = event.bytesLoaded;
            totalSize = event.bytesTotal;
            return;
        }// end function

        protected function createDelayTimer(param1:Number) : void
        {
            delayTimer = new Timer(param1, 1);
            delayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHlr);
            return;
        }// end function

        public function set adItemInfo(param1:IAdItemInfo) : void
        {
            _adItemInfo = param1;
            return;
        }// end function

        public function set adThrowInfo(param1:IAdThrowInfo) : void
        {
            _adThrowInfo = param1;
            return;
        }// end function

        public function load(param1:Number = 0) : void
        {
            if (_adItemInfo == null)
            {
                return;
            }
            removeLoader();
            createLoader();
            if (param1 > 0)
            {
                removeDelayTimer();
                createDelayTimer(param1);
                delayTimer.start();
            }
            else
            {
                startLoad();
            }
            return;
        }// end function

        protected function startLoad() : void
        {
            loader.load(new URLRequest(_adItemInfo.url), new LoaderContext(true, null, SecurityDomain.currentDomain));
            return;
        }// end function

        public function get adItemInfo() : IAdItemInfo
        {
            return _adItemInfo;
        }// end function

        protected function ioErrorHandler(event:Event) : void
        {
            return;
        }// end function

        protected function timerCompleteHlr(event:TimerEvent) : void
        {
            removeDelayTimer();
            startLoad();
            return;
        }// end function

        public function get additionalAd() : DisplayObject
        {
            return null;
        }// end function

        protected function removeDelayTimer() : void
        {
            if (delayTimer != null)
            {
                delayTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHlr);
                delayTimer.stop();
                delayTimer = null;
            }
            return;
        }// end function

        protected function createLoader() : void
        {
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            return;
        }// end function

        protected function removeLoader() : void
        {
            if (loader != null)
            {
                loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
                loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
                try
                {
                    loader.close();
                }
                catch (error:Error)
                {
                }
                loader = null;
            }
            return;
        }// end function

    }
}
